package jp.sourceforge.akjrcp.util.directorybased;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import jp.sourceforge.akjrcp.potrace.FreehepUtils;
import jp.sourceforge.akjrcp.util.ProcessPrinter;
import jp.sourceforge.akjrcp.util.image.ImageUtils;

public class PostScriptToImageCommand implements DirectoryBasedCommand{
private File srcName;
private File destName;
private String imageType;
private int width;
private int height;
BufferedImage bgImage;
private float lineTransparent=1.0f;
private float imageTransparent=1.0f;
private int backgroundColor=0xffffff;
private int blur=1;//TODO
public float getImageTransparent() {
	return imageTransparent;
}
public void setImageTransparent(float imageTransparent) {
	this.imageTransparent = imageTransparent;
}
public float getLineTransparent() {
	return lineTransparent;
}
public void setLineTransparent(float lineTransparent) {
	this.lineTransparent = lineTransparent;
}
public int getHeight() {
	return height;
}
public void setHeight(int height) {
	this.height = height;
}
public BufferedImage getImage() {
	return bgImage;
}
public void setImage(BufferedImage image) {
	this.bgImage = image;
}
public int getWidth() {
	return width;
}
public void setWidth(int width) {
	this.width = width;
}
public PostScriptToImageCommand(File srcName, File destName,String imageType){
	this.srcName=srcName;
	this.destName=destName;
	this.imageType=imageType;
}
	public int exec() throws IOException {
		if(srcName==null || destName==null){
			return -1;
		}
		
		BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
		
		Graphics2D g2=(Graphics2D) image.getGraphics();
		g2.setBackground(new Color(backgroundColor));
		g2.clearRect(0, 0, width, height);
		if(bgImage!=null){
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, imageTransparent));
			if(blur>1){
				g2.drawImage(ImageUtils.getBluredImage(bgImage,blur),-blur,-blur,null);	
			}else{
			g2.drawImage(bgImage,0,0,null);
			}
		}
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, lineTransparent));//use tp!
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
		g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
		
		FreehepUtils.drawPostScript(g2,image.getWidth(),image.getHeight(),srcName);
		
		if(imageType.toLowerCase().equals("jpg")||imageType.toLowerCase().equals("jpeg")){
		ImageUtils.writeJpegWithQuality(image, destName, 1.0f);//max quality	
		}else{
		ImageIO.write(image, imageType, destName);
		}
		return 0;
	}

	public Process getProcess() {
		
		return null;
	}

	public void setProcessPrinter(ProcessPrinter processPrinter) {
		try {
			processPrinter.printOutput("postscript to "+imageType+" from "+srcName+" to "+destName+"\r\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public int getBackgroundColor() {
		return backgroundColor;
	}
	public void setBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
	}
	public int getBlur() {
		return blur;
	}
	public void setBlur(int blur) {
		this.blur = blur;
	}

}
